home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Taiji Applet Pack v2.7 / ImageViewer / ImageViewer.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-11-06  |  12.9 KB  |  581 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Button;
  4. import java.awt.Choice;
  5. import java.awt.Color;
  6. import java.awt.Component;
  7. import java.awt.Container;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.LayoutManager;
  11. import java.awt.MediaTracker;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.ItemEvent;
  15. import java.awt.event.ItemListener;
  16. import java.awt.event.MouseEvent;
  17. import java.awt.event.MouseListener;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20. import java.net.URLEncoder;
  21. import java.util.EventObject;
  22.  
  23. public class ImageViewer extends Applet implements Runnable, ActionListener, MouseListener, ItemListener {
  24.    // $FF: renamed from: wi int
  25.    private int field_0;
  26.    // $FF: renamed from: he int
  27.    private int field_1;
  28.    private Color backCol;
  29.    private int currentImage;
  30.    private Image backImage;
  31.    private String backImageName;
  32.    private Color backColor;
  33.    private Image[] image;
  34.    private String[] imageNames;
  35.    private String[] imageFilesNames;
  36.    private Button buttonPlay;
  37.    private Button buttonStop;
  38.    private Button buttonPrev;
  39.    private Button buttonNext;
  40.    private String playLabel;
  41.    private String stopLabel;
  42.    private String prevLabel;
  43.    private String nextLabel;
  44.    private boolean[] isLoaded;
  45.    private Choice list;
  46.    private boolean loaded;
  47.    private Image buffer;
  48.    // $FF: renamed from: b java.awt.Graphics
  49.    private Graphics field_2;
  50.    private Image fond;
  51.    private Thread thread;
  52.    private int pause;
  53.    private int titleWidth;
  54.    private int titleHeight;
  55.    private int space;
  56.    private String statusBarText;
  57.    private int wiIm;
  58.    private int heIm;
  59.    private boolean isSlide;
  60.    private int pause2;
  61.    private AudioClip sound;
  62.    private boolean loopSound;
  63.    private int autoSlide;
  64.    private boolean isBackImage;
  65.  
  66.    public String getAppletInfo() {
  67.       return "Name: ImageViewer\r\nAuthor: Taiji Software";
  68.    }
  69.  
  70.    public void register() {
  71.       try {
  72.          URL u = new URL("http://www.taijisoftware.com");
  73.          ((Applet)this).getAppletContext().showDocument(u, "_blank");
  74.       } catch (Exception e) {
  75.          System.out.println(e);
  76.          this.stop();
  77.       }
  78.    }
  79.  
  80.    public void init() {
  81.       String codeBase = null;
  82.  
  83.       try {
  84.          codeBase = (new URL(((Applet)this).getCodeBase().toString())).getHost();
  85.          System.out.println("Copyright 1999, 2001 Taiji Software(tm)\nYour domain name is : " + codeBase);
  86.          codeBase = codeBase.toUpperCase();
  87.       } catch (Exception var10) {
  88.       }
  89.  
  90.       if (!((Applet)this).getCodeBase().toString().toUpperCase().startsWith("FILE") || ((Applet)this).getParameter("debug") != null) {
  91.          String regCode = ((Applet)this).getParameter("registration_code");
  92.          if (regCode == null) {
  93.             regCode = ((Applet)this).getParameter("reg_domain");
  94.             if (regCode == null) {
  95.                this.register();
  96.             } else {
  97.                if (regCode.length() == codeBase.length() + 4 && !codeBase.startsWith("WWW.")) {
  98.                   codeBase = "WWW." + codeBase;
  99.                } else if (regCode.length() == codeBase.length() - 4 && codeBase.startsWith("WWW.")) {
  100.                   codeBase = codeBase.substring(4);
  101.                }
  102.  
  103.                char[] chars = new char[codeBase.length()];
  104.                codeBase.getChars(0, codeBase.length(), chars, 0);
  105.                String key = new String("haricot");
  106.                char[] chars2 = new char[key.length()];
  107.                key.getChars(0, key.length(), chars2, 0);
  108.  
  109.                for(int i = 0; i < codeBase.length(); ++i) {
  110.                   int j;
  111.                   if (i >= key.length()) {
  112.                      j = i - key.length() * (i / key.length());
  113.                   } else {
  114.                      j = i;
  115.                   }
  116.  
  117.                   chars[i] += chars2[j];
  118.                   chars[i] = (char)(chars[i] - chars[i] / 26 * 26 + 97);
  119.                }
  120.  
  121.                String res = new String(chars);
  122.                if (!res.equalsIgnoreCase(regCode)) {
  123.                   this.register();
  124.                }
  125.             }
  126.          } else if (!regCode.equals("settevercsedegnamiaj") && !regCode.equals("8078")) {
  127.             this.register();
  128.          }
  129.       }
  130.  
  131.       this.getParameters();
  132.       ((Container)this).setLayout((LayoutManager)null);
  133.       this.fond = ((Component)this).createImage(this.field_0, this.field_1);
  134.       this.buffer = ((Component)this).createImage(this.field_0, this.field_1);
  135.       this.field_2 = this.buffer.getGraphics();
  136.       if (!this.isBackImage) {
  137.          ((Component)this).setBackground(this.backColor);
  138.       } else {
  139.          this.backImage = ((Applet)this).getImage(((Applet)this).getCodeBase(), this.backImageName);
  140.          MediaTracker tracker = new MediaTracker(this);
  141.          tracker.addImage(this.backImage, 0);
  142.  
  143.          try {
  144.             tracker.waitForAll();
  145.             this.loaded = !tracker.isErrorAny();
  146.          } catch (Exception e) {
  147.             System.err.println(e);
  148.          }
  149.  
  150.          if (!this.loaded) {
  151.             System.out.println("problem while loading background image...");
  152.             this.stop();
  153.          }
  154.       }
  155.  
  156.       ((Component)this).repaint();
  157.       this.buttonPlay = new Button(this.playLabel);
  158.       this.buttonPlay.addActionListener(this);
  159.       ((Container)this).add(this.buttonPlay);
  160.       this.buttonStop = new Button(this.stopLabel);
  161.       this.buttonStop.addActionListener(this);
  162.       ((Container)this).add(this.buttonStop);
  163.       this.buttonNext = new Button(this.nextLabel);
  164.       this.buttonNext.addActionListener(this);
  165.       ((Container)this).add(this.buttonNext);
  166.       this.buttonPrev = new Button(this.prevLabel);
  167.       this.buttonPrev.addActionListener(this);
  168.       ((Container)this).add(this.buttonPrev);
  169.       int maxLength = this.buttonPlay.getPreferredSize().width + this.buttonStop.getPreferredSize().width + this.buttonNext.getPreferredSize().width + this.buttonPrev.getPreferredSize().width;
  170.       int space = (int)((float)(this.field_0 - maxLength) / 7.0F);
  171.       this.list = new Choice();
  172.       this.list.addItemListener(this);
  173.       this.isLoaded = new boolean[this.imageNames.length];
  174.       this.image = new Image[this.imageNames.length];
  175.  
  176.       for(int i = 0; i < this.imageNames.length; ++i) {
  177.          this.list.addItem(this.imageNames[i]);
  178.          this.isLoaded[i] = false;
  179.       }
  180.  
  181.       this.list.select(this.imageNames[0]);
  182.       ((Container)this).add(this.list);
  183.       this.buttonPrev.setBounds(2 * space, this.field_1 - this.list.getPreferredSize().height - this.buttonPrev.getPreferredSize().height - 20, this.buttonPrev.getPreferredSize().width, this.buttonPrev.getPreferredSize().height);
  184.       this.buttonPlay.setBounds(3 * space + this.buttonPrev.getPreferredSize().width, this.field_1 - this.list.getPreferredSize().height - this.buttonPrev.getPreferredSize().height - 20, this.buttonPlay.getPreferredSize().width, this.buttonPlay.getPreferredSize().height);
  185.       this.buttonStop.setBounds(4 * space + this.buttonPrev.getPreferredSize().width + this.buttonPlay.getPreferredSize().width, this.field_1 - this.list.getPreferredSize().height - this.buttonPrev.getPreferredSize().height - 20, this.buttonStop.getPreferredSize().width, this.buttonStop.getPreferredSize().height);
  186.       this.buttonNext.setBounds(5 * space + this.buttonPrev.getPreferredSize().width + this.buttonPlay.getPreferredSize().width + this.buttonStop.getPreferredSize().width, this.field_1 - this.list.getPreferredSize().height - this.buttonPrev.getPreferredSize().height - 20, this.buttonNext.getPreferredSize().width, this.buttonNext.getPreferredSize().height);
  187.       this.list.setBounds((this.field_0 - this.list.getPreferredSize().width) / 2, this.field_1 - this.list.getPreferredSize().height - 10, this.list.getPreferredSize().width, this.list.getPreferredSize().height);
  188.       this.initPaint();
  189.       if (this.sound != null) {
  190.          if (this.loopSound) {
  191.             this.sound.loop();
  192.          } else {
  193.             this.sound.play();
  194.          }
  195.       }
  196.  
  197.       ((Component)this).repaint();
  198.       if (this.autoSlide == 1) {
  199.          this.actionPerformed(new ActionEvent(this.buttonPlay, 1001, "play"));
  200.       }
  201.  
  202.    }
  203.  
  204.    public void getParameters() {
  205.       this.field_0 = ((Component)this).getSize().width;
  206.       this.field_1 = ((Component)this).getSize().height;
  207.       this.backImageName = ((Applet)this).getParameter("background_image");
  208.       if (this.backImageName != null) {
  209.          this.isBackImage = true;
  210.       }
  211.  
  212.       if (this.getColor("background_color") != null) {
  213.          this.backColor = this.getColor("background_color");
  214.       }
  215.  
  216.       String s = ((Applet)this).getParameter("play_label");
  217.       if (s != null) {
  218.          this.playLabel = s;
  219.       }
  220.  
  221.       s = ((Applet)this).getParameter("stop_label");
  222.       if (s != null) {
  223.          this.stopLabel = s;
  224.       }
  225.  
  226.       s = ((Applet)this).getParameter("prev_label");
  227.       if (s != null) {
  228.          this.prevLabel = s;
  229.       }
  230.  
  231.       s = ((Applet)this).getParameter("next_label");
  232.       if (s != null) {
  233.          this.nextLabel = s;
  234.       }
  235.  
  236.       s = ((Applet)this).getParameter("pause2");
  237.       if (s != null) {
  238.          this.pause2 = Integer.parseInt(s);
  239.       }
  240.  
  241.       s = ((Applet)this).getParameter("auto_slide");
  242.       if (s != null) {
  243.          this.autoSlide = Integer.parseInt(s);
  244.       }
  245.  
  246.       s = ((Applet)this).getParameter("status_bar_text");
  247.       if (s != null) {
  248.          this.statusBarText = s;
  249.       }
  250.  
  251.       int n;
  252.       for(n = 1; ((Applet)this).getParameter("image" + Integer.toString(n)) != null; ++n) {
  253.       }
  254.  
  255.       this.imageNames = new String[n - 1];
  256.       this.imageFilesNames = new String[n - 1];
  257.  
  258.       for(int i = 0; i < this.imageNames.length; ++i) {
  259.          this.imageFilesNames[i] = ((Applet)this).getParameter("image" + Integer.toString(i + 1));
  260.          this.imageNames[i] = ((Applet)this).getParameter("image_name" + Integer.toString(i + 1));
  261.          if (this.imageNames[i] == null || this.imageNames[i].equals("")) {
  262.             int P = this.imageFilesNames[i].length();
  263.  
  264.             for(int p = -1; (p = this.imageFilesNames[i].indexOf(".", p + 1)) != -1; P = p) {
  265.             }
  266.  
  267.             this.imageNames[i] = this.imageFilesNames[i].substring(0, P);
  268.          }
  269.       }
  270.  
  271.       s = ((Applet)this).getParameter("sound_name");
  272.       if (s != null) {
  273.          this.sound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  274.       }
  275.  
  276.       s = ((Applet)this).getParameter("loop_sound");
  277.       if (s != null) {
  278.          if (s.equals("yes")) {
  279.             this.loopSound = true;
  280.             return;
  281.          }
  282.  
  283.          this.loopSound = false;
  284.       }
  285.  
  286.    }
  287.  
  288.    private Color getColor(String param) {
  289.       String s = ((Applet)this).getParameter(param);
  290.       if (s != null) {
  291.          if (s.substring(0, 1).equals("#")) {
  292.             s = s.substring(1);
  293.             int i = Integer.parseInt(s, 16);
  294.             return new Color(i);
  295.          } else {
  296.             return null;
  297.          }
  298.       } else {
  299.          return null;
  300.       }
  301.    }
  302.  
  303.    public final void paint(Graphics g) {
  304.       if (!this.isBackImage || this.loaded) {
  305.          this.field_2.drawImage(this.fond, 0, 0, this);
  306.          if (this.wiIm < this.titleWidth && this.heIm < this.titleHeight) {
  307.             this.field_2.drawImage(this.image[this.currentImage], (this.titleWidth - this.wiIm) / 2 + 10, (this.titleHeight - this.heIm) / 2 + this.space, this);
  308.          } else if (this.wiIm > this.titleWidth && this.heIm < this.titleHeight) {
  309.             float rap = (float)this.titleWidth / (float)this.wiIm;
  310.             this.field_2.drawImage(this.image[this.currentImage], 10, (int)(((float)this.titleHeight - (float)this.heIm * rap) / 2.0F) + this.space, this.titleWidth, (int)((float)this.heIm * rap), this);
  311.          } else if (this.wiIm < this.titleWidth && this.heIm > this.titleHeight) {
  312.             float rap = (float)this.titleHeight / (float)this.heIm;
  313.             this.field_2.drawImage(this.image[this.currentImage], (int)(((float)this.titleWidth - (float)this.wiIm * rap) / 2.0F) + 10, this.space, (int)((float)this.wiIm * rap), this.titleHeight, this);
  314.          } else {
  315.             float raph = (float)this.titleHeight / (float)this.heIm;
  316.             float rapw = (float)this.titleWidth / (float)this.wiIm;
  317.             if (raph < rapw) {
  318.                this.field_2.drawImage(this.image[this.currentImage], (int)(((float)this.titleWidth - (float)this.wiIm * raph) / 2.0F) + 10, this.space, (int)((float)this.wiIm * raph), this.titleHeight, this);
  319.             } else {
  320.                this.field_2.drawImage(this.image[this.currentImage], 10, (int)(((float)this.titleHeight - (float)this.heIm * rapw) / 2.0F) + this.space, this.titleWidth, (int)((float)this.heIm * rapw), this);
  321.             }
  322.          }
  323.  
  324.          g.drawImage(this.buffer, 0, 0, this);
  325.       }
  326.    }
  327.  
  328.    public final void update(Graphics g) {
  329.       this.paint(g);
  330.    }
  331.  
  332.    public void run() {
  333.       Thread.currentThread().setPriority(4);
  334.       int pauseSum = 0;
  335.  
  336.       while(true) {
  337.          ((Component)this).repaint();
  338.  
  339.          try {
  340.             Thread.sleep((long)this.pause);
  341.          } catch (InterruptedException var2) {
  342.          }
  343.  
  344.          if (this.isSlide) {
  345.             pauseSum += this.pause;
  346.          }
  347.  
  348.          if (pauseSum > this.pause2) {
  349.             pauseSum = 0;
  350.             this.actionPerformed(new ActionEvent(this.buttonNext, 1001, "next"));
  351.             this.isSlide = true;
  352.          }
  353.       }
  354.    }
  355.  
  356.    public void initPaint() {
  357.       Graphics e = this.fond.getGraphics();
  358.       if (this.isBackImage) {
  359.          int wiIm = this.backImage.getWidth(this);
  360.          int heIm = this.backImage.getHeight(this);
  361.          int w = this.field_0 / wiIm + 1;
  362.          int h = this.field_1 / heIm + 1;
  363.  
  364.          for(int i = 1; i <= h; ++i) {
  365.             for(int j = 1; j <= w; ++j) {
  366.                e.drawImage(this.backImage, (j - 1) * wiIm, (i - 1) * heIm, this);
  367.             }
  368.          }
  369.       } else {
  370.          e.setColor(this.backColor);
  371.          e.fillRect(0, 0, this.field_0, this.field_1);
  372.       }
  373.  
  374.       e.setColor(Color.black);
  375.       this.space = this.buttonPrev.getLocation().y / 12;
  376.       e.fillRect(10, this.space, this.field_0 - 20, 10 * this.space);
  377.       e.dispose();
  378.       this.titleWidth = this.field_0 - 20;
  379.       this.titleHeight = 10 * this.space;
  380.    }
  381.  
  382.    public void actionPerformed(ActionEvent evt) {
  383.       if (((EventObject)evt).getSource() instanceof Button) {
  384.          Button b = (Button)((EventObject)evt).getSource();
  385.          if (b.getLabel().equals(this.playLabel)) {
  386.             this.currentImage = this.list.getSelectedIndex();
  387.             if (!this.isLoaded[this.currentImage]) {
  388.                this.image[this.currentImage] = ((Applet)this).getImage(((Applet)this).getCodeBase(), this.imageFilesNames[this.currentImage]);
  389.                MediaTracker tracker = new MediaTracker(this);
  390.                tracker.addImage(this.image[this.currentImage], 0);
  391.  
  392.                try {
  393.                   tracker.waitForAll();
  394.                   this.loaded = !tracker.isErrorAny();
  395.                } catch (Exception e) {
  396.                   System.err.println(e);
  397.                }
  398.  
  399.                this.isLoaded[this.currentImage] = true;
  400.             }
  401.  
  402.             this.wiIm = this.image[this.currentImage].getWidth(this);
  403.             this.heIm = this.image[this.currentImage].getHeight(this);
  404.             ((Component)this).repaint();
  405.             this.isSlide = true;
  406.             return;
  407.          }
  408.  
  409.          if (b.getLabel().equals(this.stopLabel)) {
  410.             this.isSlide = false;
  411.             return;
  412.          }
  413.  
  414.          if (b.getLabel().equals(this.nextLabel)) {
  415.             if (this.isSlide) {
  416.                this.isSlide = false;
  417.             }
  418.  
  419.             ++this.currentImage;
  420.             if (this.currentImage >= this.imageNames.length) {
  421.                this.currentImage = 0;
  422.             }
  423.  
  424.             this.list.select(this.imageNames[this.currentImage]);
  425.             if (!this.isLoaded[this.currentImage]) {
  426.                this.image[this.currentImage] = ((Applet)this).getImage(((Applet)this).getCodeBase(), this.imageFilesNames[this.currentImage]);
  427.                MediaTracker tracker = new MediaTracker(this);
  428.                tracker.addImage(this.image[this.currentImage], 0);
  429.  
  430.                try {
  431.                   tracker.waitForAll();
  432.                   this.loaded = !tracker.isErrorAny();
  433.                } catch (Exception e) {
  434.                   System.err.println(e);
  435.                }
  436.  
  437.                this.isLoaded[this.currentImage] = true;
  438.             }
  439.  
  440.             this.wiIm = this.image[this.currentImage].getWidth(this);
  441.             this.heIm = this.image[this.currentImage].getHeight(this);
  442.             ((Component)this).repaint();
  443.             return;
  444.          }
  445.  
  446.          if (b.getLabel().equals(this.prevLabel)) {
  447.             if (this.isSlide) {
  448.                this.isSlide = false;
  449.             }
  450.  
  451.             --this.currentImage;
  452.             if (this.currentImage < 0) {
  453.                this.currentImage = this.imageNames.length - 1;
  454.             }
  455.  
  456.             this.list.select(this.imageNames[this.currentImage]);
  457.             if (!this.isLoaded[this.currentImage]) {
  458.                this.image[this.currentImage] = ((Applet)this).getImage(((Applet)this).getCodeBase(), this.imageFilesNames[this.currentImage]);
  459.                MediaTracker tracker = new MediaTracker(this);
  460.                tracker.addImage(this.image[this.currentImage], 0);
  461.  
  462.                try {
  463.                   tracker.waitForAll();
  464.                   this.loaded = !tracker.isErrorAny();
  465.                } catch (Exception e) {
  466.                   System.err.println(e);
  467.                }
  468.  
  469.                this.isLoaded[this.currentImage] = true;
  470.             }
  471.  
  472.             this.wiIm = this.image[this.currentImage].getWidth(this);
  473.             this.heIm = this.image[this.currentImage].getHeight(this);
  474.             ((Component)this).repaint();
  475.          }
  476.       }
  477.  
  478.    }
  479.  
  480.    public void itemStateChanged(ItemEvent e) {
  481.       if (this.isSlide) {
  482.          this.isSlide = false;
  483.       }
  484.  
  485.       this.currentImage = this.list.getSelectedIndex();
  486.       if (!this.isLoaded[this.currentImage]) {
  487.          this.image[this.currentImage] = ((Applet)this).getImage(((Applet)this).getCodeBase(), this.imageFilesNames[this.currentImage]);
  488.          MediaTracker tracker = new MediaTracker(this);
  489.          tracker.addImage(this.image[this.currentImage], 0);
  490.  
  491.          try {
  492.             tracker.waitForAll();
  493.             this.loaded = !tracker.isErrorAny();
  494.          } catch (Exception e2) {
  495.             System.err.println(e2);
  496.          }
  497.  
  498.          this.isLoaded[this.currentImage] = true;
  499.       }
  500.  
  501.       this.wiIm = this.image[this.currentImage].getWidth(this);
  502.       this.heIm = this.image[this.currentImage].getHeight(this);
  503.       ((Component)this).repaint();
  504.    }
  505.  
  506.    public void start() {
  507.       if (this.thread == null) {
  508.          this.thread = new Thread(this);
  509.          this.thread.start();
  510.       }
  511.  
  512.    }
  513.  
  514.    public void stop() {
  515.       if (this.thread != null) {
  516.          this.thread = null;
  517.       }
  518.  
  519.       if (this.sound != null) {
  520.          this.sound.stop();
  521.       }
  522.  
  523.    }
  524.  
  525.    public URL giveURL(String url) {
  526.       try {
  527.          if (url.toUpperCase().startsWith("HTTP")) {
  528.             return new URL(url);
  529.          } else if (url.toUpperCase().startsWith("FTP")) {
  530.             int p = url.indexOf(":");
  531.             int p2 = url.indexOf(":", p + 1);
  532.             if (p2 != -1) {
  533.                url = url.substring(0, p + 3) + URLEncoder.encode(url.substring(p + 3, url.length()));
  534.             }
  535.  
  536.             p = url.indexOf("%40");
  537.             if (p != -1) {
  538.                url = url.substring(0, p) + "@" + url.substring(p + 3, url.length());
  539.             }
  540.  
  541.             return new URL(url);
  542.          } else {
  543.             return new URL(((Applet)this).getCodeBase(), url);
  544.          }
  545.       } catch (MalformedURLException e) {
  546.          System.out.println(e);
  547.          return null;
  548.       }
  549.    }
  550.  
  551.    public ImageViewer() {
  552.       this.backColor = Color.white;
  553.       this.pause = 200;
  554.       this.pause2 = 2000;
  555.       this.playLabel = "Slide on";
  556.       this.stopLabel = "Slide off";
  557.       this.prevLabel = "Prev";
  558.       this.nextLabel = "Next";
  559.       ((Component)this).addMouseListener(this);
  560.    }
  561.  
  562.    public void mouseClicked(MouseEvent e) {
  563.    }
  564.  
  565.    public void mouseEntered(MouseEvent e) {
  566.       if (this.statusBarText != null) {
  567.          ((Applet)this).showStatus(this.statusBarText);
  568.       }
  569.  
  570.    }
  571.  
  572.    public void mouseExited(MouseEvent e) {
  573.    }
  574.  
  575.    public void mousePressed(MouseEvent e) {
  576.    }
  577.  
  578.    public void mouseReleased(MouseEvent e) {
  579.    }
  580. }
  581.